package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.behavior.mapper.ApBehaviorEntryMapper;
import com.usian.behavior.mapper.ApLikesBehaviorMapper;
import com.usian.behavior.service.ApLikesBehaviorService;
import com.usian.model.behavior.dtos.LikesBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApLikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper ,ApLikesBehavior> implements ApLikesBehaviorService {

    @Autowired
    private ApLikesBehaviorMapper apLikesBehaviorMapper;

    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;

    @Override
    public ResponseResult saveLikeBehavior(LikesBehaviorDto dto) {

        //1.检查参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询行为实体
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户未登录");
        }
        ApBehaviorEntry apBehaviorEntry= apBehaviorEntryMapper.findByUserIdOrEquipmentId(user.getId(), null);

        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"用户实体不存在");
        }

        //保存like
        ApLikesBehavior apLikesBehavior=new ApLikesBehavior();
        apLikesBehavior.setEntryId(apBehaviorEntry.getId());
        apLikesBehavior.setType(dto.getType());
        apLikesBehavior.setArticleId(dto.getArticleId());
        apLikesBehavior.setOperation(dto.getOperation());
        apLikesBehavior.setCreatedTime(new Date());
        apLikesBehaviorMapper.saveLikeBehavior(apLikesBehavior);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
